angular.module('register', [])
    .controller('registerCtrl', ['$scope', 'httpService', '$localStorage', '$rootScope', 'nextPageByRole', 'alertmsg', '$location', '$interval', '$rootScope', function($scope, httpService, $localStorage, $rootScope, nextPageByRole, alertmsg, $location, $interval, $rootScope) {
        $scope.user = {
            email: $location.search().email
        };

        $scope.fromEmail = $location.search().token;

        if(!isweixin) {
            var data_save = $localStorage.data_save;
            console.log('data_save', data_save)
            if(data_save && typeof data_save == 'object' && data_save.data_type == 'register') {
                $scope.user = $localStorage.data_save;
            }
        }

        $scope.$watch('user', function(a, b) {
            if(isMobile) {
                a.data_type = 'register';
                if(!isweixin) {
                    $localStorage.data_save = a;
                }
                
            }
        });

        var regx = /(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
        $scope.count_down_time = interval;
        $scope.isDisplayTime = false;
        $scope.sendCode = function(tel) {
            if (regx.test(tel)) {
                httpService.query('web/send_code', { tel: tel }, 'post').then(function(res) {
                    if (res.code == 200) {
                        countDown();
                    } else {
                        alertmsg(res.msg);
                    }
                });
            } else {
                alertmsg('请正确输入手机号码！');
            }
        };

        // 倒计时方法
        function countDown() {
            var timer;
            // 显示倒计时时间
            $scope.isDisplayTime = true;
            timer = $interval(function() {
                $scope.count_down_time -= 1;
                // 当倒计时结束时，结束显示正常
                if ($scope.count_down_time <= 0) {
                    // 结束倒计时器
                    $interval.cancel(timer);
                    // 将倒计时重新附值
                    $scope.count_down_time = interval;
                    // 设置为可提交状态
                    $scope.isabled = false;
                    // 显示为获取验证码
                    $scope.isDisplayTime = false;
                }
            }, 1000);
        };

        // 注册
        var firstsubmit = true;
        $scope.isregister = false;
        $scope.isargee = true;
        $scope.register = function(bool, user) {
            user.token = $location.search().token;
            if (firstsubmit) {
                if (!bool) {
                    firstsubmit = false;
                    httpService.query('web/sign_up', user, 'post').then(function(res) {
                        firstsubmit = true;
                        if (res.code == 200) {
                            $rootScope.token = res.token;
                            $rootScope.user = res.user;
                            $localStorage.mutian_token = res.token;
                            $localStorage.user = res.user;
                            $localStorage.data_save = '';
                            nextPageByRole.go(res.user.role);
                        } else {
                            alertmsg(res.msg);
                        }
                    }, function() {
                        firstsubmit = true;
                    });
                } else {
                    $scope.isregister = true;
                }

            } else {
                alertmsg('请不要多次提交！');
            }

        };
    }]);
